/*
 * ================================================================
 * Copyright 2014-2018 Sensetime.
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Sensetime Corp. Ltd, ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Sensetime.
 * ================================================================
 *  创建人: Hyatt
 *	创建时间: 2018年6月28日下午4:54:54
 */
package com.sm.tool.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * 日期工具类
 *
 * @author liqitian
 * @createDate 2018-12-27
 */
public class DateUtil {

	private static final String BLANK = " ";
	
	private static final String OPERATOR_T = "T";
	
	/**
	 * <p>
	 * 将指定时间java.util.Date转换为java.time.LocalTime
	 * </p>
	 * 
	 * @param date
	 * @return LocalTime 格式->17:00:26.994
	 */
	public static LocalTime parseDateToLocalTime(Date date) {
		Instant instant = date.toInstant();
		LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
		return localDateTime.toLocalTime();
	}

	/**
	 * <p>
	 * 将指定时间java.util.Date转换为java.time.LocalDate
	 * </p>
	 * 
	 * @param date
	 * @return LocalDate 格式->2018-06-28
	 */
	public static LocalDate parseDateToLocalDate(Date date) {
		Instant instant = date.toInstant();
		LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
		return localDateTime.toLocalDate();
	}

	/**
	 * 
	 * <p>
	 * 将毫秒级别时间转换为java.time.LocalDate
	 * </p>
	 * @param timeMillis
	 * @return
	 */
	public static LocalDate parseLongToLocalDate(long timeMillis) {
		Date date = new Date(timeMillis);
		Instant instant = date.toInstant();
		LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
		return localDateTime.toLocalDate();
	}
	
	/**
	 * 
	 * <p>
	 * 将毫秒级别时间转换为java.time.LocalDateTime
	 * </p>
	 * @param timeMillis
	 * @return
	 */
	public static LocalDateTime parseLongToLocalDateTime(long timeMillis) {
		Date date = new Date(timeMillis);
		Instant instant = date.toInstant();
		return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
	}
	
	/**
	 * 将时间字符串转换成java.time.LocalDateTime<br>
	 * 字符串形如：
	 * <br>2011-12-03T10:15:30
	 * <br>2011-12-03 10:15:30
	 * <br>2011-12-03
	 * @param yyyyMMddTHHmmss
	 * @return
	 */
	public static LocalDateTime parseStringToLocalDateTime(String yyyyMMddTHHmmss) {
		DateTimeFormatter formatter = null;
		if (yyyyMMddTHHmmss.contains(BLANK)) {
			formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		} else if (yyyyMMddTHHmmss.contains(OPERATOR_T)) {
			formatter = DateTimeFormatter.ISO_DATE_TIME;
		} else {
			formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		}
		LocalDateTime localDateTime = LocalDateTime.parse(yyyyMMddTHHmmss, formatter);
		return localDateTime;
	}
	
	/**
	 * 将时间字符串转换成java.time.LocalTime<br>
	 * 字符串形如：
	 * <br>10:15
	 * <br>10:15:30
	 * <br>10:15:30.00
	 * @param hhmmss
	 * @return
	 */
	public static LocalTime parseStringToLocalTime(String hhmmss) {
		return LocalTime.parse(hhmmss);
	}
	
	/**
	 * 将时间戳转换成yyyy-MM-dd HH:mm:ss格式字符串
	 * @param milliseconds
	 * @return
	 */
	public static String parseLongToyyyyMMddHHmmss(long milliseconds) {
		LocalDateTime localDateTime = parseLongToLocalDateTime(milliseconds);
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		return localDateTime.format(formatter);
	}

	/**
	 * <p>
	 * 获取两个日期之间的日期列表，包含首尾日期
	 * </p>
	 *
	 * @param startDate
	 * @param endDate
	 * @return java.util.List<java.time.LocalDate>
	 * @throws
	 */
	public static List<LocalDate> getBetweenDates(String startDate, String endDate) {
		LocalDate localDate = LocalDate.parse(startDate, DateTimeFormatter.ISO_DATE);
		LocalDate localDate1 = LocalDate.parse(endDate, DateTimeFormatter.ISO_DATE);
		Period between = Period.between(localDate, localDate1);
		List<LocalDate> list = new ArrayList<>();
		for (int i = 0; i <= between.getDays(); i++) {
			list.add(localDate.plusDays(i));
		}
		return list;
	}

}
